import { catelist, delcate,editcate } from '@/api/goods/goods.api'
import { Message, MessageBox } from 'element-ui';
import Vue from 'vue'
const vue = new Vue()
export default {
    namespaced: true,
    state() {
        return {
            categoryList: [],
            total: 10,
            currentPage: 1,
            pageSize: 5,
        }
    },
    getters: {
        change_editAble(state) {
            return state.categoryList.map(item => {
                vue.$set(item, 'editAble', false)
                return item
            })
        }

    },
    mutations: {
        update_categoryList(state, payload) {
            state.categoryList = [...payload]
        },
        update_currentPage(state, payload) {
            state.currentPage = payload
        },
        update_pageSize(state, payload) {
            state.pageSize = payload
        },
        update_total(state, payload) {
            state.total = payload
        },
    },
    actions: {
        sync_update_categoryList({ state, commit }) {
            let { currentPage, pageSize } = state
            console.log({ currentPage, pageSize });
            catelist({ currentPage, pageSize }).then(res => {
                commit('update_categoryList', res.data.data)
                commit('update_total', res.data.total)
            })
        },

        sync_editcate({dispatch},data){
            editcate(data).then(res=>{
                console.log(res);
                dispatch('sync_update_categoryList')
            })
        },
        sync_del_category({ dispatch },id) {
            MessageBox.confirm('此操作将永久删除该分类, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delcate({id}).then(res => {
                    
                    dispatch('sync_update_categoryList')
                })
                Message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                Message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        }
    },
    modules: {}



}